// useProductData.ts
import { useMemo } from "react";
import { Product } from "@/business.InterfaceLayer/type";

const useProductData = (filteredProducts: Product[]) => {
  const getUniqueValues = (products: Product[], key: any) => {
    const allValues = products.map((product: any) => product[key]).flat();
    return Array.from(new Set(allValues));
  };

  const uniqueColors = useMemo(
    () => getUniqueValues(filteredProducts, "colors"),
    [filteredProducts]
  );
  const uniqueBrands = useMemo(
    () => getUniqueValues(filteredProducts, "brand"),
    [filteredProducts]
  );

  const maxPrice = useMemo(() => {
    return filteredProducts.reduce(
      (max, product) => Math.max(max, product.price),
      0
    );
  }, [filteredProducts]);

  const colorCounts = useMemo(
    () =>
      uniqueColors.map(
        (color) =>
          filteredProducts.filter((product) => product.colors.includes(color))
            .length
      ),
    [filteredProducts, uniqueColors]
  );

  const brandCounts = useMemo(
    () =>
      uniqueBrands.map(
        (brand) =>
          filteredProducts.filter((product) => product.brand === brand).length
      ),
    [filteredProducts, uniqueBrands]
  );

  return { uniqueColors, uniqueBrands, colorCounts, brandCounts, maxPrice };
};

export default useProductData;

// const uniqueColors = useMemo(
//   () => Array.from(new Set(filteredProducts.map((product) => product.colors).flat())),
//   [filteredProducts]
// );

// const uniqueBrands = useMemo(
//   () => Array.from(new Set(filteredProducts.map((product) => product.brand))),
//   [filteredProducts]
// );
